package observer.a;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class WeatherData implements Subject {
	
	private List<Observer> observers = new ArrayList<Observer>();
	
	private float temperature;
	
	private float humidity;
	
	private float pressure;
	
	private WeatherDataObject data = new WeatherDataObject();

	public void notifyAllObservers() {
		for (Iterator<Observer> ite = observers.iterator(); ite.hasNext();) {
			Observer observer = ite.next();
			observer.update(this,data);
		}
	}

	public void registerObserver(Observer observer) {
		observers.add(observer);
	}

	public void removeObserver(Observer observer) {
		int i = observers.indexOf(observer);
		if (i >= 0) {
			observers.remove(i);
		}
	}
	
	public void measurementsChanged() {
		notifyAllObservers();
	}

	/******************************************************************************/
	
	public void setMeasurements(float temperature,float humidity,float pressure) {
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		
		data.setTemperature(temperature);
		data.setHumidity(humidity);
		data.setPressure(pressure);
	}
}
